import { doGetGoodsDetailsCouponPage } from '@/pluginPackage/coupon/apis'
import type { CartApiCouponVO, ApiCouponVO } from '@/apis/plugin/coupon/model'

interface GetCouponListParams {
  shopId: Long
  amount: string
  productId: Long
}
export const goodsDetailCouponScrollXList = ref<CartApiCouponVO[]>([])
const pageConfig = reactive({ size: 5, current: 1, pages: 1 })
/**
 * 横向滚动优惠券列表
 * @param {*} isLoad
 * @param {GetCouponListParams} options shopId amount productId
 */
export async function initGoodsDetailCouponScrollXList(isLoad = false, options: GetCouponListParams) {
  if (!isLoad) {
    // 刷新
    pageConfig.current = 1
    goodsDetailCouponScrollXList.value = await getCouponList(options)
  } else if (isLoad && pageConfig.current < pageConfig.pages) {
    // 更新
    pageConfig.current++
    goodsDetailCouponScrollXList.value = [...goodsDetailCouponScrollXList.value, ...(await getCouponList(options))]
  }
}
/**
 * 获取优惠券列表
 * @param {GetCouponListParams} options
 * @param {number} pages
 */
async function getCouponList(options: GetCouponListParams) {
  // loading.value = true
  const { code, data } = await doGetGoodsDetailsCouponPage({ ...options, current: pageConfig.current, size: pageConfig.size })
  if (code !== 200) {
    // loading.value = false
    return []
  }
  // loading.value = false
  pageConfig.pages = data.pages
  return data.records.map((item: ApiCouponVO) => ({
    ...item,
    watermark: false,
  }))
}
